#include "GLES2FBOMultiRenderTarget.h"

namespace PVM
{
	GLES2FBOMultiRenderTarget::GLES2FBOMultiRenderTarget(GLES2FBOManager* manager, const std::string& name) :
		MultiRenderTarget(name),
		fbo(manager, 0)
	{
	}
	
	GLES2FBOMultiRenderTarget::~GLES2FBOMultiRenderTarget()
	{
	}
	
	void GLES2FBOMultiRenderTarget::bindSurfaceImpl(size_t attachment, RenderTexture* target)
	{
		GLES2FrameBufferObject* fbobj = 0;
		target->getCustomAttribute("FBO", &fbobj);
		assert(fbobj);
		fbo.bindSurface(attachment, fbobj->getSurface(0));
		
		mWidth = fbo.getWidth();
		mHeight = fbo.getHeight();
	}
	
	void GLES2FBOMultiRenderTarget::unbindSurfaceImpl(size_t attachment)
	{
		fbo.unbindSurface(attachment);
		
		mWidth = fbo.getWidth();
		mHeight = fbo.getHeight();
	}
	
	void GLES2FBOMultiRenderTarget::getCustomAttribute(const std::string& name, void* pData)
	{
		if (name == "FBO")
		{
			*static_cast<GLES2FrameBufferObject**>(pData) = &fbo;
		}
	}
	
	bool GLES2FBOMultiRenderTarget::attachDepthBuffer(DepthBuffer* depthBuffer)
	{
		bool result;
		if ((result = MultiRenderTarget::attachDepthBuffer(depthBuffer)))
			fbo.attachDepthBuffer(depthBuffer);
		
		return result;
	}
	
	void GLES2FBOMultiRenderTarget::detachDepthBuffer()
	{
		fbo.detachDepthBuffer();
		MultiRenderTarget::detachDepthBuffer();
	}
	
	void GLES2FBOMultiRenderTarget::_detachDepthBuffer()
	{
		fbo.detachDepthBuffer();
		MultiRenderTarget::_detachDepthBuffer();
	}
}
